$(function(){
    //login
    $(".login-btn").click(function(){
        var telephone = $(".l-telephone").val();
        var password = $(".l-password").val();
        var telExp = /^[1][0-9][0-9]{9}$/;
        if(telephone == '' || telephone.length != 11 || !telExp.test(telephone)){
            layerBox("请输入正确的手机号码");
            return false;
        }else if(password == ''){
            layerBox("请输入正确的密码");
            return false;
        }else{
            $("#login-form").submit();
            // //success
            // var index = layer.load(1, {
            //     shade: [0.1,'#fff'] //0.1透明度的白色背景
            // });
            // $.ajax({
            //     url:"/home/login",
            //     type:"POST",
            //     data:{_token:token,phone:telephone,pass:password},
            //     success:function(msg){
            //         layer.close(index);
            //         var obj = eval("("+msg+")");
            //         if(obj['code'] == 1)
            //         {
            //             layerBox(obj['msg']);
            //             setTimeout(function(){
            //                 location.href = "/home/index";
            //             },1000);
            //         }else if(obj['code'] == -1){
            //             layerBox(obj['msg']);
            //         }
            //     },
            //     error:function(){
            //         layer.close(index);
            //         layerBox("服务器繁忙,请稍后重试!");
            //     }
            // });

        }
    });

    // register
    //获取手机验证码
    $('.yzmBtn').click(function(){
        var myreg = /^[1][0-9][0-9]{9}$/;
        var mobile = $('.r-telephoner').val();
        if( mobile == "" || !myreg.test(mobile) || mobile.length != 11){
            layerBox("请输入正确的手机号码")
        }else {
            Kyz($(".yzmBtn"),"重新发送",60);
            var index = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url:"/user/sendsms",
                type:"POST",
                data:{_token:token,phone:mobile},
                success:function(msg){
                    layer.close(index);
                    var obj = eval("("+msg+")");
                    if(obj['code'] == 1)
                    {
                        layerBox(obj['msg']);s
                    }else if(obj['code'] == -1){
                        layerBox(obj['msg']);
                    }
                },
                error:function(){
                    layer.close(index);
                    layerBox("服务器繁忙,请稍后重试!");
                }
            });

        }
    });

    // register
    $(".register-btn").click(function(){
        var username = $(".username").val();
        var sexval = $(".select-sex").val();
        var birthday = $(".birthday").val();
        var telephone = $(".r-telephoner").val();
        var password = $(".r-password").val();
        var pwdtwo = $(".pwd").val();
        var email = $(".email").val();
        var telExp = /^[1][0-9][0-9]{9}$/;
        var myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
        var addressId = $("#value2").val();
        var pid = $(".pid").val();
        var yzcode = $(".yzcode").val();
        if(username == ''){
            layerBox("请输入姓名")
            return false;
        }else if(sexval == 0){
            layerBox("请选择您的性别");
            return false;
        }else if(birthday == ''){
            layerBox("请选择您的出生年月");
            return false;
        }else if(addressId == ''){
            layerBox("请选择地址");
            return false;
        }else if(telephone == '' || telephone.length != 11 || !telExp.test(telephone)){
            layerBox("请输入正确的手机号码");
            return false;
        }else if(password == ''){
            layerBox("请设置您的密码");
            return false;
        }else if(pwdtwo == ''){
            layerBox("请重新输入您的密码");
            return false;
        }else if(pwdtwo != password){
            layerBox("密码不一致");
            return false;
        }else if(email == ''){
            layerBox("请输入邮箱");
            return false;
        }if(!myReg.test(email)){
            layerBox("请输入正确的邮箱");
            return false;
        }else if(yzcode == ''){
            layerBox("请输入正确的手机验证码");
            return false;
        }else{
            //success
            var index = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url:"/home/register",
                type:"POST",
                data:{_token:token,yzcode:yzcode,name:username,sex:sexval,birthday:birthday,phone:telephone,pass:password,pwdtwo:pwdtwo,email:email,addressId:addressId,pid:pid},
                success:function(msg){
                    layer.close(index);
                    var obj = eval("("+msg+")");
                    if(obj['code'] == 1)
                    {
                        layerBox(obj['msg']);
                        setTimeout(function(){
                            location.href = "/home/login";
                        },1000);
                    }else if(obj['code'] == -1){
                        layerBox(obj['msg']);
                    }
                },
                error:function(){
                    layer.close(index);
                    layerBox("服务器繁忙,请稍后重试!");
                }
            });
        }
    })
})

//验证码倒计时
function Kyz(kbt,msg2,ktime){
    kbt.attr('disabled','disabled');
    var t=setInterval(function(){
        ktime--;
        if(ktime==0){
            kbt.html(msg2);
            //这里的清除是起到了作用的。如果要在外部清除，就需要返回值。
            kbt.removeAttr('disabled');
            clearInterval(t);
        }else{
            kbt.html(ktime+"秒");
        };
    },1000);
    return t;
};

//错误提示
function layerBox(msg){
    layer.msg(msg);
}